package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.repository.ScenicCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private ScenicCommentRepository scenicCommentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    public ScenicComment save(ScenicComment scenicComment){
        //评论的评论
        if (scenicComment.getCommentType()==ScenicComment.SCENIC_COMMENT_TYPE){
            String refId = scenicComment.getRefId();
            ScenicComment scenicComment1 = scenicCommentRepository.findById(refId).get();
            scenicComment.setRefScenicComment(scenicComment1);
        }
        if (scenicComment.getCommentType()==ScenicComment.SCENIC_COMMENT_TYPE2){
            String refId = scenicComment.getRefId();
            ScenicComment scenicComment1 = scenicCommentRepository.findById(refId).get();
            scenicComment.setRefScenicComment(scenicComment1);
        }
            scenicComment.setCreateTime(new Date());
        return scenicCommentRepository.save((scenicComment));
    }

    public List<ScenicComment> getByScenicId(Long scenicId) {
        return null;
    }

    public List<ScenicComment> list(Long scenicId) {
        Query query=new Query();
        query.addCriteria(Criteria.where("ScenicDetailId").is(scenicId));

        List<ScenicComment> findComments = new ArrayList<>();

        //查出该景点的所有评论
        List<ScenicComment> scenicComments = mongoTemplate.find(query, ScenicComment.class);


        //遍历所有评论集合
        for (ScenicComment scenicComment : scenicComments) {
            List<ScenicComment> comments2=new ArrayList<>();
             List<ScenicComment> comments=new ArrayList<>();
            /*if (scenicComment.getCommentType()==ScenicComment.SCENIC_COMMENT_TYPE||scenicComment.getCommentType()==ScenicComment.SCENIC_COMMENT_TYPE2)
            {
                Query query1=new Query();
                query1.addCriteria(Criteria.where("refId").is(scenicComment.getId()));
                List<ScenicComment> subComments=mongoTemplate.find(query1,ScenicComment.class);
                if (subComments!=null){
                    System.out.println(subComments);
                    scenicComment.setScenicComments(subComments);
                    comments.add(scenicComment);
                }
            }*/
            //类型为0才是大评论
            if (scenicComment.getCommentType()==ScenicComment.SCENIC_COMMENT_TYPE_COMMENT){
                List<ScenicComment> lastSmallcomment=new ArrayList<>();
                Query smallQuery = new Query();
                smallQuery.addCriteria(Criteria.where("commentType").is(ScenicComment.SCENIC_COMMENT_TYPE2));
                List<ScenicComment> smallComments = mongoTemplate.find(smallQuery, ScenicComment.class);
                for (ScenicComment smallComment : smallComments) {
                   // System.out.println(smallComment.getMastScenicId());
                   // System.out.println(scenicComment.getId());
                    if (smallComment.getMastScenicId().equals(scenicComment.getId())){
                        String refId = smallComment.getRefId();
                        //Query refQuery=new Query();
                       // refQuery.addCriteria(Criteria.where("refId").is(refId));
                        ScenicComment refScenicComment = smallComment.getRefScenicComment();
                        //System.out.println(refScenicComment);
                        lastSmallcomment.add(smallComment);
                    }

                }



                //把大评论的图片切割放到list集合
                String photos = scenicComment.getPhotos();
                String[] split = photos.split(",");
                List<String> imgUrl = new ArrayList<>();
                for (String s : split) {
                    imgUrl.add(s);
                }

                for (ScenicComment comment : comments) {
                    if (comment.getRefId()==scenicComment.getId()){
                        comments2.add(comment);
                    }

                }
                if (comments2!=null&&comments2.size()>0){
                    scenicComment.setScenicComments(comments2);
                }
                Query subquery=new Query();
                subquery.addCriteria(Criteria.where("refId").is(scenicComment.getId()));
                List<ScenicComment> subComments = mongoTemplate.find(subquery, ScenicComment.class);




                List<ScenicComment> subComments2 = new ArrayList<>();
                for (ScenicComment subComment : subComments) {


                   /* Query grand=new Query();
                    grand.addCriteria(Criteria.where("refId").is(subComment.getId()));
                    List<ScenicComment> grandComments=mongoTemplate.find(grand,ScenicComment.class);
                    if (grandComments!=null&&grandComments.size()>0){
                        List<ScenicComment> subComments3 = new ArrayList<>();
                        for (ScenicComment grandComment : grandComments) {
                            String comment = grandComment.getComment();
                            if (comment!=null&&comment.length()!=0&&!(comment.equals(""))){
                                subComments3.add(grandComment);
                            }
                        }

                    subComment.setScenicComments(subComments3);
                    }*/



                   String str = subComment.getComment();
                   if (str!=null&&str.length()!=0&&!(str.equals(""))){
                       subComments2.add(subComment);
                   }
                }
                if(subComments2!=null){
                   scenicComment.setScenicComments(subComments2);
                }

                scenicComment.setSmallTypeComment(lastSmallcomment);

                //把图片集合设置到评论的图属性中
                scenicComment.setCoverImgUrl(imgUrl);
                
                //把大评论添加到新的集合进行返回
                findComments.add(scenicComment);
                

                

            }


        }

        return findComments;
    }

    public Integer getCommentCount(Long scenicId) {
        Query query=new Query();
        query.addCriteria(Criteria.where("ScenicDetailId").is(scenicId));

        List<ScenicComment> scenicComments = mongoTemplate.find(query, ScenicComment.class);
        List<ScenicComment> scenicComments2=new ArrayList<>();
        for (ScenicComment scenicComment : scenicComments) {
            String str = scenicComment.getComment();
            String photos = scenicComment.getPhotos();
            if (str!=null&&str.length()!=0&&!(str.equals(""))&&photos!=null){
                scenicComments2.add(scenicComment);
            }
        }

        return scenicComments2.size();
    }





    public void commentThumNum(String sid, Long uid) {
        Optional<ScenicComment> op = scenicCommentRepository.findById(sid);
        ScenicComment scenicComment = op.get();
        if (scenicComment==null){
            throw new LogicException("参数异常");
        }
        List<Long> thumbuplist = scenicComment.getThumbuplist();
        if (thumbuplist.contains(uid)){
            scenicComment.setThumbupnum(scenicComment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        }else {
            scenicComment.setThumbupnum(scenicComment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }

        scenicCommentRepository.save(scenicComment);
    }

    public ScenicComment getScenocComment(String sid) {
        Optional<ScenicComment> comment = scenicCommentRepository.findById(sid);
        ScenicComment scenicComment = comment.get();
        return scenicComment;
    }

    public Integer getCommentNum(String scenicCommentId) {
        List<ScenicComment> count=new ArrayList<>();
        Query query=new Query();
        query.addCriteria(Criteria.where("refId").is(scenicCommentId));
        List<ScenicComment> OnescenicComments = mongoTemplate.find(query, ScenicComment.class);
        for (ScenicComment onescenicComment : OnescenicComments) {
            String comment = onescenicComment.getComment();
            if (comment!=null&&comment.length()>0&&!(comment.equals(""))){
                count.add(onescenicComment);
            }
        }
        Query query2=new Query();
        query2.addCriteria(Criteria.where("mastScenicId").is(scenicCommentId));
        List<ScenicComment> TwoscenicComments = mongoTemplate.find(query2, ScenicComment.class);
        for (ScenicComment twoscenicComment : TwoscenicComments) {
            String comment = twoscenicComment.getComment();
            if (comment!=null&&comment.length()>0&&!(comment.equals(""))){
                count.add(twoscenicComment);
            }
        }
        System.out.println(count.size());
        return count.size();
    }

    public Integer getThumbuoNum(String scenicCommentId) {
        Optional<ScenicComment> comment = scenicCommentRepository.findById(scenicCommentId);
        ScenicComment scenicComment = comment.get();
        //点赞数
        int thumbupnum = scenicComment.getThumbupnum();
        return thumbupnum;
    }




 /*   public List<ScenicComment> listSmall(Long scenicId) {
        List<ScenicComment> smallScenicComments = new ArrayList<>();
        List<ScenicComment> finallySmallComment = new ArrayList<>();

        Query query=new Query();
        query.addCriteria(Criteria.where("commentTyep").is(ScenicComment.SCENIC_COMMENT_TYPE2));
        List<ScenicComment> scenicComments = mongoTemplate.find(query, ScenicComment.class);
        for (ScenicComment scenicComment : scenicComments) {
            if(scenicComment.getScenicDetailId()==scenicId){
                smallScenicComments.add(scenicComment);
            }
        }
        List<ScenicComment> list = this.list(scenicId);
        for (ScenicComment scenicComment : list) {
            for (ScenicComment smallScenicComment : smallScenicComments) {
                if (smallScenicComment.getMastScenicId()==scenicComment.getId()){
                    finallySmallComment.add(smallScenicComment);
                }
            }
        }
        return null;
    }*/
}
